package cn.com.cym.gamezao.entity.authorize;


import cn.com.cym.gamezao.annotation.Sensitive;
import cn.com.cym.gamezao.base.BaseEntity;
import cn.com.cym.gamezao.config.SensitiveStrategy;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

@Setter
@Getter
@NoArgsConstructor
@Table(name = "a_user_info",uniqueConstraints = @UniqueConstraint(columnNames="user_id"))
@Entity
@Accessors(chain = true)
@ApiModel(description = "用户基本信息")
public class UserInfo extends BaseEntity implements Serializable {
    private static final long serialVersionUID = 8008283468846191312L;


    /**
     * 用户姓名
     */
    @ApiModelProperty(value = "姓名",required = true,dataType = "String")
    @Column(length = 36)
    private String name;


    /**
     * 昵称
     */
    @ApiModelProperty(value = "昵称",required = true,dataType = "String")
    @Column(length = 36)
    private String nikeName;


    /**
     * 图标，头像
     */
    @Column(length = 64)
    @ApiModelProperty(value = "图标，头像",dataType = "String")
    private String headPic;

    @Transient
    private MultipartFile fileStr;

    /**
     * 性别 0 男 1 女  现在在elementUi中使用的是字符串 显示，无法使用number类型
     */
    @ApiModelProperty(value = "性别",required = true,dataType = "String ")
    @Column(length = 2)
    private String sex;


    @OneToOne(targetEntity= User.class)
    @JoinColumn(name = "user_id")
    @ApiModelProperty(value = "用户主表信息",required = true,dataType = "GameUser")
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private User user;


    /**
     * 出生日期
     */
    @Column
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "出生日期",required = true,dataType = "LocalDate")
    private LocalDate birthDate;

    /**
     * 证件类型
     */
    @Column(length = 36)
    @ApiModelProperty(value = "证件类型",required = true,dataType = "String")
    private String cardType;

    /**
     * 证件编码
     */
    @Column(length = 18)
    @ApiModelProperty(value = "证件编码",required = true,dataType = "String")
    @Sensitive(strategy = SensitiveStrategy.ID_CARD)
    private String cardCode;

    @Transient
    private Integer age;

    public Integer getAge(){
        if(this.birthDate == null){
            return null;
        }else{
            Long until = this.birthDate.until(LocalDate.now(), ChronoUnit.YEARS);
            return until.intValue();
        }
    }
}
